package com.atguigu.gulimall.member.service.impl;

import com.atguigu.common.exception.BizCodeEnum;
import com.atguigu.common.exception.RRException;
import com.atguigu.common.vo.MemberRsepVo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.member.dao.UmsMemberDao;
import com.atguigu.gulimall.member.entity.UmsMemberEntity;
import com.atguigu.gulimall.member.service.UmsMemberService;


@Service("umsMemberService")
public class UmsMemberServiceImpl extends ServiceImpl<UmsMemberDao, UmsMemberEntity> implements UmsMemberService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UmsMemberEntity> page = this.page(
                new Query<UmsMemberEntity>().getPage(params),
                new QueryWrapper<UmsMemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveData(UmsMemberEntity umsMember) {
        //校验手机号和用户名重复
        QueryWrapper<UmsMemberEntity> queryWrapper=new QueryWrapper<UmsMemberEntity>().eq("username",umsMember.getUsername()).or().eq("mobile",umsMember.getMobile());
        int count = this.count(queryWrapper);
        if(count<0){
            new RRException(BizCodeEnum.POHNE_USER_EXCEPTION.getMsg(),BizCodeEnum.POHNE_USER_EXCEPTION.getCode());
        }else{
            //生成md5加密密码
            BCryptPasswordEncoder bCryptPasswordEncoder=new BCryptPasswordEncoder();
            String password = bCryptPasswordEncoder.encode(umsMember.getPassword());
            umsMember.setPassword(password);
            this.save(umsMember);
        }
    }

    @Override
    public MemberRsepVo login(UmsMemberEntity umsMember) {
        QueryWrapper<UmsMemberEntity> queryWrapper=new QueryWrapper<UmsMemberEntity>().eq("username",umsMember.getMobileOrName()).or().eq("mobile",umsMember.getMobileOrName());
        UmsMemberEntity umsMemberEntity = this.getOne(queryWrapper);
        if(umsMemberEntity!=null){
            BCryptPasswordEncoder bCryptPasswordEncoder=new BCryptPasswordEncoder();
            boolean matches = bCryptPasswordEncoder.matches(umsMember.getPassword(), umsMemberEntity.getPassword());
            if(matches){
                MemberRsepVo memberRsepVo=new MemberRsepVo();
                BeanUtils.copyProperties(umsMemberEntity,memberRsepVo);
                memberRsepVo.setPassword(null);
                return memberRsepVo;
            }else{
                return null;
            }
        }else{
            new RRException(BizCodeEnum.POHNE_NAME_EXCEPTION.getMsg(),BizCodeEnum.POHNE_NAME_EXCEPTION.getCode());
            return null;
        }
    }

}